package com.audaque.springboot.foshanupload.cloudauthtrans.config;

import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.cloudauthtrans.interceptor.AuthHeaderTransInterceptor;
import com.audaque.springboot.foshanupload.cloudauthtrans.interceptor.LoginMdcHandlerInterceptor;
import com.audaque.springboot.foshanupload.cloudauthtrans.interceptor.SessionIdHeaderInterceptor;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.core.properties.StaticResourcePathProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-04 00:01:38
 */

@Configuration
public class CloudAuthTransInterceptorConfig implements WebMvcConfigurer {

  @Autowired
  private StaticResourcePathProperties staticResourcePathProperties;

  @Autowired
  private SessionIdHeaderInterceptor sessionIdHeaderInterceptor;

  @Autowired
  private AuthHeaderTransInterceptor authHeaderTransInterceptor;
  @Autowired
  private LoginMdcHandlerInterceptor loginMdcHandlerInterceptor;

  @Autowired
  private LoginProperties loginProperties;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {


	//表示要拦截
	String[] addPathPatterns = {
			ComConst.INTERCEPTOR_ANY_MATCH
	};


	//要排除的路径
	List<String> excludePathPatterns = staticResourcePathProperties.getList();
	List<String> unAuthenticatedUrlList = loginProperties.getUnAuthenticatedUrlList();
	unAuthenticatedUrlList.addAll(excludePathPatterns);
	String excludedUris = String.join(",", unAuthenticatedUrlList);


	//用注解识别是否需要登录，而不是路径
	registry.addInterceptor(sessionIdHeaderInterceptor).addPathPatterns(addPathPatterns)
	.excludePathPatterns(excludedUris)
	.order(100);

	//用注解识别是否需要登录，而不是路径
	registry.addInterceptor(authHeaderTransInterceptor).addPathPatterns(addPathPatterns)
	.excludePathPatterns(excludedUris)
	.order(100);

	registry.addInterceptor(loginMdcHandlerInterceptor).addPathPatterns(addPathPatterns)
	.excludePathPatterns(excludedUris)
	.order(101);


  }


}

